package com.zhd.grpcclient.service;

import com.zhd.grepcommon.user.BooleanReply;
import com.zhd.grepcommon.user.User;
import com.zhd.grepcommon.user.UserServiceGrpc;
import net.devh.springboot.autoconfigure.grpc.client.GrpcClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import io.grpc.Channel;

/**
 * @Author: zhanghuadi
 * @CreateDate: 2019/3/9 0009 15:42
 * @UpdateDate: 2019/3/9 0009 15:42
 * @UpdateRemark:
 * @Version: 1.0
 * @Description:
 */
@Service
public class UserGrpcService {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserGrpcService.class);

    @GrpcClient("service-grpc")
    private Channel serverChannel;

    public String addUser(){

        //远程调用
        UserServiceGrpc.UserServiceBlockingStub stub = UserServiceGrpc.newBlockingStub(serverChannel);
        BooleanReply response = stub.addUser(User.newBuilder()
                .setId("10001")
                .setUsername("zhanghuadi")
                .setAddress("CHENGDU")
                .setAge(22)
                .setSex(1)
                .setCreatetime(System.currentTimeMillis()+"")
                .setUpdatetime(System.currentTimeMillis()+"")
                .build());

        //获取调用结果
        LOGGER.info("grpc-client received: ---> " + response.getReply());
        if(response.getReply()){
            return "success";
        }else{
            return "fail";
        }
    }
}
